package com.heptafish.mapeditor.items
{
	import com.heptafish.mapeditor.utils.ImageLoader;
	import com.heptafish.mapeditor.utils.MapEditorConstant;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filters.GlowFilter;
	
	public class Building extends BaseDisplayObject
	{
		static public var  KEY_UP:int = 38;
		static public var  KEY_DOWN:int = 40;
		static public var  KEY_LEFT:int= 37;
		static public var  KEY_RIGHT:int = 39;
		
		public var id:int;
		public var _bitMap:Bitmap;//显示图像
		private var _configXml:XML;//配置
		public var init:Boolean = false;
		private var _imageLoader:ImageLoader;
		private var moveMode:Boolean=false;
		public var myFilter:GlowFilter = 
			new GlowFilter(0xFF0000, 0.8, 3, 3, 10, 1, false, false);
		private var selected:Boolean=false;
		
		private var mouse_xoff:int;
		private var mouse_yoff:int;
		public function Building(pid:* = null)
		{
			this.id = pid;
			this.addEventListener(MouseEvent.CLICK,onMouseClick);
			
			
		}
		private function onMouseClick(evt:MouseEvent){
		 
			if(evt.ctrlKey){
				moveMode=!moveMode;
			 
				if(moveMode){
				  this.filters=[myFilter];	
				  stage.addEventListener(KeyboardEvent.KEY_DOWN,doMove);
				  stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
				  stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
				  this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				}else{
					this.filters=null;
					stage.removeEventListener(KeyboardEvent.KEY_DOWN,doMove);
					stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
					stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
					this.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				}
			}
		}
		private function onMouseDown(evt:MouseEvent){
			mouse_xoff=evt.stageX-x;
			mouse_yoff=evt.stageY-y;
			selected=true;
			
		}
		private function onMouseUp(evt:MouseEvent){
			selected=false;
			
		}
		private function onMouseMove(evt:MouseEvent){
			trace("moveMode "+moveMode );
			trace("selected "+selected );
			if(moveMode&&selected){
				this.x=evt.stageX-mouse_xoff;
				this.y=evt.stageY-mouse_yoff;
				configXml.@px = x;
				configXml.@py = y;
			}
			
		}
		private function doMove(evt:KeyboardEvent){
			if(evt.keyCode==KEY_UP){
				this.y--;
			}
			if(evt.keyCode==KEY_DOWN){
				this.y++;
			}
			if(evt.keyCode==KEY_LEFT){
				this.x--;
			}
			if(evt.keyCode==KEY_RIGHT){
				this.x++;
			}
			configXml.@px = x;
			configXml.@py = y;
		}
		public function setBitMap(bitMapData:BitmapData):void{
			_bitMap = new Bitmap(bitMapData);
			addChild(_bitMap);
		}
		
		public function reset(bitMapData:BitmapData,configXml:XML):void{
			if(numChildren > 0)
				removeChildAt(0);
			_bitMap = new Bitmap(bitMapData);
			addChild(_bitMap);
			_configXml = configXml;
		}
		
		public function get configXml():XML{
			return _configXml;
		}
		public function set configXml(configXml:XML):void{
			_configXml = configXml;
		}
		public function imageLoaded(evet:Event):void{
//			var file:File = File(evet.target);
//			_bitMap = HeptaFishImageUtils.ByteArrayToBitmap(file.data);
//			file.removeEventListener(Event.COMPLETE,this.imageLoaded);
			_bitMap = new Bitmap(_imageLoader.data);
			addChild(_bitMap);
			_imageLoader.removeEventListener(Event.COMPLETE,imageLoaded);
			_imageLoader = null;
//			HeptaFishGC.gc();
		}
		
		public function loadImage():void{
			_imageLoader = new ImageLoader();
			_imageLoader.addEventListener(Event.COMPLETE,imageLoaded);
			_imageLoader.load(File(MapEditorConstant.COMPONENT_LIB_HOME.resolvePath(_configXml.@file[0])).nativePath);
		}
		

	}
}